package com.pig4cloud.mall.product.controller;

import com.pig4cloud.mall.product.dto.CategoryParam;
import com.pig4cloud.mall.product.service.ProductCategoryService;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;


/**
 * 商品分类
 *
 * @author fangyu
 * @date 2020-12-26 23:41:08
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/category")
@Api(value = "product-category", tags = "商品分类管理")
public class ProductCategoryController {

	private final ProductCategoryService productCategoryService;

	/**
	 * 添加商品分类
	 *
	 * @param categoryParam 产品分类
	 * @return R
	 */
	@ApiOperation(value = "添加产品分类", notes = "添加商品分类")
	@SysLog("添加商品分类")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('product_category_add')")
	public R save(@RequestBody CategoryParam categoryParam) {
		int count = productCategoryService.create(categoryParam);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("添加失败!");
		}
	}

	/**
	 * 根据上级id查询子分类
	 */
	@ApiOperation(value = "根据上级id查询子分类", notes = "根据上级id查询子分类")
	@GetMapping("/list/{parentId}")
	@PreAuthorize("@pms.hasPermission('product_category_get')")
	public R getProductCategory(@PathVariable Long parentId) {
		return R.ok(productCategoryService.listCategory(parentId));
	}

	/**
	 * 分页查询商品分类
	 */
	@ApiOperation(value = "分页查询商品分类", notes = "分页查询商品分类")
	@GetMapping("/page/{parentId}")
	@PreAuthorize("@pms.hasPermission('product_category_get')")
	public R getProductCategoryPage(@PathVariable Long parentId,
									@RequestParam(value = "page", defaultValue = "1") Integer page,
									@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize
	) {
		return R.ok(productCategoryService.pageCategory(parentId, page, pageSize));
	}


	/**
	 * 根据id获取商品分类
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "根据id获取商品分类", notes = "根据id获取商品分类")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('product_productcategory_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(productCategoryService.getById(id));
	}

	/**
	 * 修改商品分类
	 *
	 * @param categoryParam 商品分类请求参数
	 * @return R
	 */
	@ApiOperation(value = "修改商品分类", notes = "修改商品分类")
	@SysLog("修改商品分类")
	@PostMapping("/update/{id}")
	@PreAuthorize("@pms.hasPermission('product_category_edit')")
	public R updateById(@PathVariable("id") Long id, @RequestBody CategoryParam categoryParam) {
		int count = productCategoryService.update(id, categoryParam);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("更新失败!");
		}
	}

	/**
	 * 删除商品分类
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "删除商品分类", notes = "删除商品分类")
	@SysLog("删除商品分类")
	@PostMapping("/delete/{id}")
	@PreAuthorize("@pms.hasPermission('product_category_del')")
	public R delete(@PathVariable Long id) {
		int count = productCategoryService.delete(id);
		if (count > 0) {
			return R.ok(count);
		} else {
			return R.failed("删除失败!");
		}
	}

}
